<div class="container-fluid" style="margin-top: 10px;">

    <div class="row">
        <div class="col-sm-12">
            {:form('text',['title'=>'用户','value'=>$mngUserData.USER_NAME,'disable'=>'disabled'])}
            <input hidden name="USER_ID" value="{$mngUserData.USER_ID}">
            {:form('text',['title'=>'角色','value'=>$mngUserData.ROLE_NAME,'disable'=>'disabled'])}
            <input hidden name="ROLE_ID" value="{$mngUserData.ROLE_ID}">

            {:line()}
            <div class="col-sm-6">
                <label>隶属单位数据范围</label>
                <table id="belong_table" class="table" ></table>
            </div>
            <div class="col-sm-6">
                <label>学院数据范围</label>
                <table id="college_table" class="table" ></table>
            </div>
            <div class="col-sm-12" style="text-align: center;margin-top: 10px;">
                <button id="addButton" class="btn btn-info">提交</button>
            </div>
        </div>

    </div>

</div>

<script>

    $(function () {

        $("#belong_table").bootstrapTable({
            url:"{:url('dict/getBelongList')}",
            method:'post',
            columns:[{
                checkbox:true
            },{
                title:'单位',
                field:'BELONG'
            },{
                title:'BELONG_ID',
                field:'BELONG_ID',
                visible: false,
            }],
            queryParams:function (params) {
                return params;
            },
            responseHandler:function (res) {
                // res = JSON.parse(res);
                console.log(res);
                setTimeout(function () {
                    console.log("{$mngUserData.BELONG_IDS}")
                    if("{$mngUserData.BELONG_IDS}" == '*') {
                        $("#belong_table").bootstrapTable('checkAll');
                    } else {
                        var belongIds = "{$mngUserData.BELONG_IDS}".split(',');
                        $("#belong_table").bootstrapTable('checkBy',{field:'BELONG_ID',values:belongIds});
                    }

                },100);
                return res;
            },
        });

        $("#college_table").bootstrapTable({
            url:"{:url('dict/getCollege')}",
            method:'post',
            height:400,
            columns:[{
                checkbox:true,
                clickToSelect:true
            },{
                title:'学院',
                field:'SHORTNAME'
            },{
                title:'COLLEGE_ID',
                field:'COLLEGE_ID',
                visible: false,
            }],
            queryParams:function (params) {
                return params;
            },
            responseHandler:function (res) {
                // res = JSON.parse(res);
                console.log(res);
                setTimeout(function () {
                    if("{$mngUserData.COLLEGE_IDS}" == '*') {
                        $("#college_table").bootstrapTable('checkAll');
                    } else {
                        var collegeIds = "{$mngUserData.COLLEGE_IDS}".split(',');
                        $("#college_table").bootstrapTable('checkBy',{field:'COLLEGE_ID',values:collegeIds});
                    }
                },100);
                return res;
            }
        });

        $("#addButton").on('click',function () {
            var userId = $("[name='USER_ID']").val();
            var roleId = $("[name='ROLE_ID']").val();
            if (userId.length==0) {
                layer.alert('请选择用户',{icon:2});
                return;
            }
            if (roleId.length==0) {
                layer.alert('请选择角色',{icon:2});
                return;
            }

            var belongList = $("#belong_table").bootstrapTable("getSelections");
            if(belongList.length < 1){
                layer.alert('请勾选隶属单位数据范围',{icon:2});
                return;
            }
            var belongIds = [];
            for(var i = 0 ; i < belongList.length ; i++){
                belongIds.push(belongList[i]['BELONG_ID']);
            }
            console.log(belongIds);

            var collegeList = $("#college_table").bootstrapTable("getSelections");
            if(collegeList.length < 1){
                layer.alert('请勾选学院数据范围',{icon:2});
                return;
            }
            var collegeIds = [];
            for(var i = 0 ; i < collegeList.length ; i++){
                collegeIds.push(collegeList[i]['COLLEGE_ID']);
            }
            console.log(collegeIds);

            layer.confirm('确认提交管理员信息吗？',{icon:1},function (index) {
                layer.close(index);
                ajax("{:url('edit')}",{userId:userId,roleId:roleId,belongIds:belongIds,collegeIds:collegeIds},function (data) {
                    // console.log(data);
                    parent.location.reload();
                })
            })
        });

    });


</script>